package com.hevs.adibilis;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONException;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class DetailActivityActivity extends Activity implements OnClickListener {

	private ArrayList<HashMap<String, String>> details;

	TextView nameActivityTextView;
	TextView descriptionActivityTextView;
	CheckBox accesCheckBox;
	CheckBox wcCheckBox;
	CheckBox parkingCheckBox;
	Button addressButton;
	Button navigateToButton;
	ImageView imageView;
	
	//gabioud - 03.12.2013
	TextView addressActivityTextView;
	TextView localityActivityTextView;
	TextView phoneActivityTextView;
	TextView eMailActivityTextView;
	LoaderImageView activityImage;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_detail_activity);
		
		nameActivityTextView = (TextView) findViewById(R.id.textViewNameActivity);
		descriptionActivityTextView = (TextView) findViewById(R.id.textViewDescriptionActivity);
		accesCheckBox = (CheckBox) findViewById(R.id.checkBoxAcces);
		wcCheckBox = (CheckBox) findViewById(R.id.CheckBoxWc);
		parkingCheckBox = (CheckBox) findViewById(R.id.CheckBoxParking);
		addressButton = (Button) findViewById(R.id.addressButton);
		addressButton.setOnClickListener(this);
		navigateToButton = (Button) findViewById(R.id.navigateToButton);
		navigateToButton.setOnClickListener(this);
		imageView = (ImageView) findViewById(R.id.imageView1);
		
		//gabioud - 12.03.2013
		addressActivityTextView = (TextView) findViewById(R.id.AddressTextView);
		localityActivityTextView = (TextView) findViewById(R.id.NPAAndCityTextView);
		phoneActivityTextView = (TextView) findViewById(R.id.TelefonTextView);
		eMailActivityTextView = (TextView) findViewById(R.id.textViewEMail);
		activityImage = (LoaderImageView) findViewById(R.id.loaderImageView);
		accesCheckBox.setText("");
		wcCheckBox.setText("");
		parkingCheckBox.setText("");
		
		// getting intent data
        Intent in = getIntent();
        
        // Get JSON values from previous intent
        String id = in.getStringExtra(JSONFactory.TAG_ID);

		if(Utils.hasInternet(this)){
			new DownloadDataFromWeb().execute(id);
		} else {
			Toast.makeText(this, R.string.PleaseConnectToInternet, Toast.LENGTH_LONG).show();
		}
        
		
	}

	

	// Uses AsyncTask to create a task away from the main UI thread. This task takes a 
	// URL string and uses it to create an HttpUrlConnection. Once the connection
	// has been established, the AsyncTask downloads the contents of the webpage as
	// an InputStream. Finally, the InputStream is converted into a string, which is
	// displayed in the UI by the AsyncTask's onPostExecute method.
	private class DownloadDataFromWeb extends AsyncTask<String, String, ArrayList<HashMap<String, String>>> {


		// onPostExecute displays the results of the AsyncTask.
		protected void onPostExecute(ArrayList<HashMap<String, String>> result) {

				details = result;
			
				nameActivityTextView.setText(result.get(0).get(JSONFactory.TAG_NAME));
				descriptionActivityTextView.setText(result.get(0).get(JSONFactory.TAG_DESCRIPTION));
				
				
				//gabioud - 12.03.2013
				addressActivityTextView.setText(result.get(0).get(JSONFactory.TAG_ADDRESS));
				localityActivityTextView.setText(result.get(0).get(JSONFactory.TAG_NPA));
				phoneActivityTextView.setText(result.get(0).get(JSONFactory.TAG_TELEPHONE));
				eMailActivityTextView.setText(result.get(0).get(JSONFactory.TAG_EMAIL));
				String img_Url = result.get(0).get(JSONFactory.TAG_IMAGE_ID)+"."+result.get(0).get(JSONFactory.TAG_IMAGE);
				activityImage.setImageDrawable("http://adibilis.businessinformationsystem.ch/Symfony/web/img/" + img_Url);
				
				
				Log.d("url", "http://adibilis.businessinformationsystem.ch/Symfony/web/img/" + img_Url);
				
				
				if(result.get(0).get(JSONFactory.TAG_ACCESS).equals("1"))
					accesCheckBox.setChecked(true);
				
				if(result.get(0).get(JSONFactory.TAG_WC).equals("1"))
					wcCheckBox.setChecked(true);
				
				
				
				if(result.get(0).get(JSONFactory.TAG_PARKING).equals("1"))
					parkingCheckBox.setChecked(true);
				
				//imageView.setImageResource(getResources().getIdentifier("i_" + activityList.get(0).get(TAG_IMAGE_ID), "drawable", getPackageName()));
				

		}

		@Override
		protected ArrayList<HashMap<String, String>> doInBackground(String... idString) {


			try {
				JSONFactory.getDetailActivity(idString[0]);
				return JSONFactory.parseDetailActivity();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (NoNodesException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return null;
		}
	}


	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.detail_activity, menu);
		return true;
	}

	@Override
	public void onClick(View v) {

		if(v == this.navigateToButton){
		
			Log.d("TAG", "Navigation vers google maps");

			//Uri uri = Uri.parse("geo:" + activityList.get(0).get(TAG_LATITUDE) + "," + activityList.get(0).get(TAG_LONGITUDE) + "?q="+ activityList.get(0).get(TAG_LATITUDE) + "," + activityList.get(0).get(TAG_LONGITUDE));
			//TODO redondance
			/*double latitude = Double.parseDouble(details.get(0).get(JSONFactory.TAG_LATITUDE));
			double longitude = Double.parseDouble(details.get(0).get(JSONFactory.TAG_LONGITUDE));
			String label = details.get(0).get(JSONFactory.TAG_NAME);
			String uriBegin = "geo:" + latitude + "," + longitude;
			String query = latitude + "," + longitude + "(" + label + ")";
			String encodedQuery = Uri.encode(query);
			String uriString = uriBegin + "?q=" + encodedQuery + "&z=2";
			Uri uri = Uri.parse(uriString);
			
			Intent intent = new Intent(Intent.ACTION_VIEW, uri);
			startActivity(intent);*/
			
			String uri = "geo:"+Double.parseDouble(details.get(0).get(JSONFactory.TAG_LATITUDE))+","+Double.parseDouble(details.get(0).get(JSONFactory.TAG_LONGITUDE));
			Log.d("geo", uri);
			
			Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
			startActivity(intent);
			
			/*
			Intent intent = new Intent(android.content.Intent.ACTION_VIEW, 
				    Uri.parse("http://maps.google.com/maps?q="+Double.parseDouble(details.get(0).get(JSONFactory.TAG_LATITUDE))+","+Double.parseDouble(details.get(0).get(JSONFactory.TAG_LONGITUDE))));
				startActivity(intent);*/
			
			
			
		}
		
		
			
		
	}

}
